package com.itmd.service.impl;

import com.itmd.domain.Account;
import com.itmd.mapper.AccountMapper;
import com.itmd.service.AccountService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;


@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;


    @Override
    public void save(Account account) throws IOException {

        accountMapper.save(account);

//        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapconfig.xml");
//
//        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
//
//        SqlSession sqlSession = build.openSession(true);
//
//        AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
//
//        mapper.save(account);

    }

    @Override
    public List<Account> findAll() throws IOException {

//          原始方式
//        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapconfig.xml");
//
//        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
//
//        SqlSession sqlSession = build.openSession(true);
//
//        AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
//
//        List<Account> all = mapper.findAll();
//
//        return all;


//        mybattis 整合ssm方式
        List<Account> all = accountMapper.findAll();
        return all;
    }

}
